﻿using GrapeCity.Forguncy.CellTypes;
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Vant.Properties;
using System.Windows;
using Vant.WpfControls.DrawingObject;

namespace Vant.CellTypes
{
    public class NumberPickerDesigner : InputCellTypeBaseDesigner<NumberPicker>
    {
        public override void InitDefaultPropertyValues(IBuilderContext context)
        {
            base.InitDefaultPropertyValues(context);
            this.CellType.placeholder = Resources.Input_PlaceEnter;
            this.CellType.NumberPickerConfig.closeButtonText = Resources.Number_NumberPickerConfig_CloseButtonText_DefaultValue;
        }

        public override FrameworkElement GetDrawingControl(ICellInfo cellInfo, IDrawingHelper drawingHelper)
        {
            InputDrawingObject inputDrawing = base.GetDrawingControl(cellInfo, drawingHelper) as InputDrawingObject;
            if (cellInfo.HorizontalAlignment == ForguncyCellHorizontalAlignment.General)
            {
                inputDrawing.ViewModel.HorizontalAlignment = HorizontalAlignment.Right;
            }
            return inputDrawing;
        }
    }

    [Designer("Vant.CellTypes.NumberPickerDesigner, Vant")]
    [Icon("pack://application:,,,/Vant;component/Resources/Images/Number.png")]
    [OrderWeight((int)Utils.OrderWeight.Number)]
    public class NumberPicker : InputCellTypeBase
    {
        public override ForguncyCellHorizontalAlignment DefaultAlignment => ForguncyCellHorizontalAlignment.Right;
        public override List<Command> CommandList { get => base.CommandList; set => base.CommandList = value; }
        public override DataValidationLink DataValidationLink { get => base.DataValidationLink; set => base.DataValidationLink = value; }
        public override List<UIPermission> UIPermissions { get => base.UIPermissions; set => base.UIPermissions = value; }
        public override object DefaultValue { get => base.DefaultValue; set => base.DefaultValue = value; }

        [SRDisplayName(nameof(Resources.Input_Maxlength))]
        [FormulaProperty]
        public object maxlength { get; set; }

        public override string placeholder { get => base.placeholder; set => base.placeholder = value; }
        public override ImageValue leftIcon { get => base.leftIcon; set => base.leftIcon = value; }
        public override ImageValue rightIcon { get => base.rightIcon; set => base.rightIcon = value; }
        public override bool border { get => base.border; set => base.border = value; }
        public override bool clearable { get => base.clearable; set => base.clearable = value; }

        [DefaultValue(true)]
        [SRDisplayName(nameof(Resources.Number_ConvertToNumber))]
        [SRDescription(nameof(Resources.Number_ConvertToNumber_Description))]
        public bool convertToNumber { get; set; } = true;
        public override bool ReadOnly { get => base.ReadOnly; set => base.ReadOnly = value; }
        public override bool IsDisabled { get => base.IsDisabled; set => base.IsDisabled = value; }


        [ObjectProperty(ObjType = typeof(NumberPickerConfig))]
        [SRDisplayName(nameof(Resources.Number_NumberPickerConfig))]
        public NumberPickerConfig NumberPickerConfig { get; set; } = new NumberPickerConfig();

        public override bool NeedFormatDefaultValue => true;

        [SRDisplayName(nameof(Resources.Number_ShowPopup))]
        [RunTimeMethod]
        public void ShowPopup()
        {
        }

        public override string ToString()
        {
            return Properties.Resources.Number_DisplayName;
        }
    }

    public class NumberPickerConfig : ObjectPropertyBase
    {
        [SRDisplayName(nameof(Resources.Number_NumberPickerConfig_Title))]
        public string title { get; set; }

        [SRDisplayName(nameof(Resources.Number_NumberPickerConfig_CloseButtonText))]
        [SRDescription(nameof(Resources.Number_NumberPickerConfig_CloseButtonText_Descrption))]
        public string closeButtonText { get; set; }

        [SRDisplayName(nameof(Resources.Number_NumberPickerConfig_DeleteButtonText))]
        [SRDescription(nameof(Resources.Number_NumberPickerConfig_DeleteButtonText_Descrption))]
        public string deleteButtonText { get; set; }

        [SRDisplayName(nameof(Resources.Number_NumberPickerConfig_ExtraKey1))]
        [SRDescription(nameof(Resources.Number_NumberPickerConfig_ExtraKey1_Descrption))]
        public string extraKey1 { get; set; }

        [SRDisplayName(nameof(Resources.Number_NumberPickerConfig_RightPanel))]
        [SRDescription(nameof(Resources.Number_NumberPickerConfig_RightPanel_Description))]
        public bool rightPanel { get; set; }

        [SRDisplayName(nameof(Resources.Number_NumberPickerConfig_ExtraKey2))]
        [SRDescription(nameof(Resources.Number_NumberPickerConfig_ExtraKey2_Description))]
        public string extraKey2 { get; set; }

        [SRDisplayName(nameof(Resources.Number_NumberPickerConfig_HideOnClickOutside))]
        [DefaultValue(true)]
        public bool hideOnClickOutside { get; set; } = true;

        [SRDisplayName(nameof(Resources.Number_NumberPickerConfig_RandomKeyOrder))]
        public bool randomKeyOrder { get; set; }

        public override bool GetDesignerPropertyVisible(string propertyName)
        {
            if(propertyName == nameof(extraKey2))
            {
                return rightPanel;
            }
            return base.GetDesignerPropertyVisible(propertyName);
        }
    }
}
